package com.cjjd.c.feature.home.ewm;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cjjd.c.R;
import com.cjjd.c.ShareKeys;
import com.cjjd.c.base.ActionBarFragment;
import com.cjjd.c.bean.HotelQrCodeInfoBean;
import com.cjjd.c.bean.HttpResult;
import com.cjjd.c.feature.dialog.TextDialog;
import com.cjjd.c.http.CallbackImpl;
import com.cjjd.c.http.JsonUtils;
import com.cjjd.c.utils.BitmapUtil;
import com.cjjd.c.utils.DialogUtil;
import com.cjjd.c.utils.SharePrefUtil;
import com.google.zxing.WriterException;
import com.uuzuche.lib_zxing.activity.ZXingLibrary;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by dada on 2017/12/23.
 */

public class ZingFragment extends ActionBarFragment {
    @Bind(R.id.icon)
    ImageView icon;
    @Bind(R.id.name)
    TextView name;
    @Bind(R.id.id)
    TextView id;
    @Bind(R.id.layout)
    LinearLayout layout;

    HotelQrCodeInfoBean bean;
    @Override
    public Object getTitleString() {
        return "微信公众号";
    }

    @Override
    public int getContentView() {
        return R.layout.fragment_zxing;
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        layout.setVisibility(View.INVISIBLE);
        getHotelMessage();
    }

    private void getHotelMessage() {
        DialogUtil.delDialog(mContext);
        HashMap<String, Object> h1 = new HashMap<>();
        h1.put("token", SharePrefUtil.getString(mContext, ShareKeys.Token, ""));
        h1.put("sessionId", SharePrefUtil.getString(mContext, ShareKeys.sessionId, ""));
        h1.put("hotelUuid", SharePrefUtil.getString(mContext, ShareKeys.HomeHotelUuid));
        service.getHotelQrCodeInfo(JsonUtils.convertParam("getHotelQrCodeInfo", h1)).enqueue(new CallbackImpl<HotelQrCodeInfoBean>() {
            @Override
            public void responseSuccess(Call<HttpResult<HotelQrCodeInfoBean>> call, Response<HttpResult<HotelQrCodeInfoBean>> response) {
                 bean=response.body().data;

                DialogUtil.dismissLoadingDialog(mContext);
                layout.setVisibility(View.VISIBLE);
                if(TextUtils.isEmpty(bean.qrCodePath)){
                    new TextDialog(mContext, "酒店还未开通微信公众号哦～", new TextDialog.OnCloseListener() {
                        @Override
                        public void OnClick() {
                            mContext.finish();
                        }
                    }).show();
                    return;
                }

                ZX();
            }

            @Override
            public void onFailure(Call<HttpResult<HotelQrCodeInfoBean>> call, Throwable t) {
                super.onFailure(call, t);
                DialogUtil.dismissLoadingDialog(mContext);
            }

        });

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    Bitmap bitmap;
    public void ZX(){

//      Bitmap bitmap = BitmapUtil.create2DCoderBitmap(uri, mScreenWidth/2, mScreenWidth/2);
            try {
                bitmap = BitmapUtil.createQRCode(bean.qrCodePath, 300);

                if(bitmap != null){
                    icon.setImageBitmap(bitmap);
                }

            } catch (WriterException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


        name.setText(bean.hotelName);
            id.setText(bean.weChatPublicId);


    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        try{
            bitmap.recycle();
        }catch (Exception e){

        }
    }
}


